#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > energy_nevpt2_gs.info <<'%EOF%'
   energy_nevpt2_gs
   ----------------
   Molecule:         H2CO
   Wave Functions:   MCSCF (CAS) / NEVPT2 / ano-1 basis
   Test Purpose:     Check NEVPT2 energy calculation for el.ground state
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > energy_nevpt2_gs.dal <<'%EOF%'
ATOMBASIS
 Formaldehyde
 ANO basis
    3    2  X Y
       8.     1 Basis=ano-1 4 3 1
O1   0.0000000000            0.0000000000            0.0196594609
       6.     1 Basis=ano-1 4 3 1
C1   0.0000000000            0.0000000000            2.3248507925
       1.     1 Basis=ano-1 2 1
H1   0.0000000000            1.7597110083            3.3972521023

EOF

**DALTON INPUT
.RUN WAVE FUNCTION
.NEWTRA    ! test if NEVPT2 works with "new" integral transf.
**WAVE FUNCTIONS
.HF
.MP2
.MCSCF
.NEVPT2
*SCF INPUT
.DOUBLY OCCUPIED
 5 1 2 0
*CONFIGURATION INPUT
.SYMMETRY
 1
.SPIN MUL
 1
.INACTIVE
 4 0 1 0
.ELECTRONS
 6
.CAS SPACE
 2 2 1 0
*NEVPT2
.THRESHOLD
 0.D0
*OPTIMIZATION
.THRESHOLD
 1.D-9
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >energy_nevpt2_gs.check
cat >>energy_nevpt2_gs.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -i -a"
else
   GREP="egrep -i"
fi

# Reading from basis set library:
# Wave function:
CRIT1=`$GREP "Number of closed shell electrons * 10" $log | wc -l`
CRIT2=`$GREP "Number of electrons in active shells * 6" $log | wc -l`
CRIT3=`$GREP " active orbitals .* 5" $log | wc -l`
CRIT4=`$GREP "Total number of orbitals .* 46" $log | wc -l`
CRIT5=`$GREP "Spin multiplicity is .* 1" $log | wc -l`
CRIT6=`$GREP "Total number of symmetries * 4" $log | wc -l`
CRIT7=`$GREP "Reference state symmetry * 1" $log | wc -l`
TEST[1]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7`
CTRL[1]=7
ERROR[1]="INCORRECT WAVE FUNCTION"

# Symmetry:
CRIT1=`$GREP "Number of configurations * 18" $log | wc -l`
CRIT2=`$GREP "Number of orbital rotations * 139" $log | wc -l`
TEST[2]=`expr $CRIT1  \+ $CRIT2`
CTRL[2]=2
ERROR[2]="CONFIGURATION AND ORBITAL ROTATIONS NOT CORRECT"

# SCF energy:
CRIT1=`$GREP "Hartree-Fock total energy *: * \-113\.909396425[0-9]" $log | wc -l`
TEST[3]=`expr $CRIT1`
CTRL[3]=1
ERROR[3]="SCF ENERGY NOT CORRECT"

# MP2 energy:
CRIT1=`$GREP "MP2 second order energy *: * \-114\.2592103..." $log | wc -l`
TEST[4]=`expr $CRIT1`
CTRL[4]=1
ERROR[4]="MP2 ENERGY NOT CORRECT"

# MCSCF energy:
CRIT1=`$GREP "Final MCSCF energy: * \-113\.9848882376.[0-9]" $log | wc -l`
TEST[5]=`expr $CRIT1`
CTRL[5]=1
ERROR[5]="MCSCF ENERGY NOT CORRECT"

# NEVPT2 energy corrections
CRIT1=`$GREP "1 * \-113\.9848882[0-9] * \-114\.2419212[0-9] * \-114\.2448504[0-9]" $log | wc -l`
TEST[6]=`expr $CRIT1`
CTRL[6]=1
ERROR[6]="NEVPT2 energies not correct."

PASSED=1
for i in 1 2 3 4 5 6
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
